#!/bin/bash

public_hostname=$(curl -s 'http://169.254.169.254/latest/meta-data/public-hostname')

AWS_METADATA_URL="http://169.254.169.254/latest/meta-data"
function get_aws_metadata_value() {
  local path="$1"
  local value=$(curl -s -f $AWS_METADATA_URL/$path)

  if [[ $? -eq 0 ]]; then
    echo "$value"
  else
    echo ""
  fi
}


function set_aws_defaults_from_environ() {
  local zone=$(get_aws_metadata_value "/placement/availability-zone/")
  local region=${zone%?}
  local mac_addr=$(get_aws_metadata_value "/network/interfaces/macs/")
  local vpc_id=$(get_aws_metadata_value "/network/interfaces/macs/${mac_addr}vpc-id")
  local subnet_id=$(get_aws_metadata_value "/network/interfaces/macs/${mac_addr}subnet-id")
  DEFAULT_CLOUD_PROVIDER="aws"
  DEFAULT_AWS_REGION="$region"
  AWS_VPC_ID="$vpc_id"
  AWS_SUBNET_ID="$subnet_id"
  
}


function write_default_value() {
  local name="$1"
  local value="$2"

  if egrep "^$name=" /etc/default/spinnaker > /dev/null; then
    sudo sed -i.bak "s/^$name=.*/$name=$value/" /etc/default/spinnaker
  else
    sudo bash -c "echo $name=$value >> /etc/default/spinnaker"
  fi
}

set_aws_defaults_from_environ

write_default_value "SPINNAKER_AWS_DEFAULT_REGION" $DEFAULT_AWS_REGION
write_default_value "SPINNAKER_AWS_ENABLED" "true"
write_default_value "SPINNAKER_GOOGLE_ENABLED" "false"


echo
echo '######### WELCOME TO SPINNAKER ############'
echo
echo 'Initializing systems!'
echo
echo 'Default region is configured for Amazon AWS region: ' $region
echo 
echo 'GCE is disabled on this host. Refer to http://spinnaker.io/'
echo 'for more specific instructions.'
echo

# Set VPC if in a VPC
if [[ -n "$AWS_VPC_ID" ]]; then
    echo 'You are in a VPC setting this . . .'
    write_default_value "AWS_VPC_ID" $AWS_VPC_ID
    write_default_value "AWS_SUBNET_ID" $AWS_SUBNET_ID
    echo 'Amazon VPC ID is set to: '$AWS_VPC_ID
    echo
    echo 'Amazon AWS Subnet is set to: '$AWS_SUBNET_ID
    echo
fi

# Configure Deck
echo 'Reconfiguring deck . . . . '
echo
sudo /opt/spinnaker/bin/reconfigure_spinnaker.sh

# Restart Spinnaker
echo 'Restarting Spinnaker . . . '
echo
sudo restart spinnaker

## Helper text
echo
echo 'Edit /etc/default/spinnaker and restart Spinnaker if you wish to change this'
echo 'Then execute: '
echo 'sudo service clouddriver restart'
echo 'sudo service rosco restart'
echo
echo '------ You should be ready to go -----'
echo 'To connect create a SSH tunnel from your host to' $public_hostname 'remote ports 9000 and 8084'
echo
echo 'You may add this to ~/.ssh/config: '
echo 'Host spinnaker'
echo '    HostName' $public_hostname
echo '    IdentityFile /path/to/private/key'
echo '    LocalForward 9000 127.0.0.1:9000'
echo '    LocalForward 8084 127.0.0.1:8084'
echo '    User ubuntu'
echo
echo 'If the ssh config file is new. Ensure it is chmod 400'
echo
echo 'Execute: ssh -f -N spinnaker'
echo 'Open http://localhost:9000/ in your web browser'
echo
echo 'This message will now self-destruct. Enjoy'
echo

sed -i "s/\.\ \.init-region//" $HOME/.bashrc
